package com.lishe.lishe.lishe.module.myorder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.myorder.adapter.LogisticsAdapter;
import com.lishe.lishe.lishe.module.myorder.bean.LogisticsBean;
import com.lishe.lishe.lishe.module.myorder.bean.StepsBean;
import com.lishe.lishe.lishe.module.myorder.presenter.OrderPresenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ToastUtil;
import com.lishe.lishe.retrofit.DataResponseListener;
import java.util.LinkedList;

/**
 * Created by Bin on 2018/1/31.
 * 物流信息
 */
public class LogisticsActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvCompany;
    private TextView mTvNum;

    private View mEmptyLayout;
    private RecyclerView mRecyclerView;

    private String mTid;
    private LogisticsAdapter mAdapter;
    private LinkedList<StepsBean> mList;
    private View mErrorLayout;
    private View mLogisticsLayout;


    public static void jump(Activity activity, String tid) {
        Intent intent = new Intent(activity, LogisticsActivity.class);
        intent.putExtra("tid", tid);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        mTid = getIntent().getStringExtra("tid");
        initView();
        getExpressList();
    }

    private void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(this);
        TextView title = findViewById(R.id.title_text_title);
        title.setText("物流信息");

        mTvCompany = findViewById(R.id.logistics_company);
        mTvNum = findViewById(R.id.logistics_num);
        mLogisticsLayout = findViewById(R.id.logistics_layout);

        mEmptyLayout = findViewById(R.id.empty_layout);
        mRecyclerView = findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(LogisticsActivity.this));
        mList = new LinkedList<>();
        mAdapter = new LogisticsAdapter(this, mList);
        mRecyclerView.setAdapter(mAdapter);

        mErrorLayout = findViewById(R.id.error_layout);
        findViewById(R.id.error_retry).setOnClickListener(this);
    }

    private void getExpressList() {
        LoadingUtils.createLoadingDialog(this, "加载中...", true);
        OrderPresenter.getInstance().getExpressList(mTid, new DataResponseListener<LogisticsBean>() {
            @Override
            public void onSuccess(LogisticsBean bean) {
                LoadingUtils.closeDialog();
                mTvNum.setText("物流单号：" + bean.getData().getLogi_id());
                mTvCompany.setText("物流公司：" + bean.getData().getLogi_name());
                mLogisticsLayout.setVisibility(View.VISIBLE);
                if (bean.getData().getSteps() == null || bean.getData().getSteps().size() == 0) {
                    mRecyclerView.setVisibility(View.GONE);
                    mEmptyLayout.setVisibility(View.VISIBLE);
                } else {
                    mList.addAll(bean.getData().getSteps());
                    mAdapter.notifyDataSetChanged();
                    mRecyclerView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFail(String failMessage) {
                LoadingUtils.closeDialog();
                ToastUtil.showToast(LogisticsActivity.this, failMessage);
            }

            @Override
            public void onError(Throwable e) {
                LoadingUtils.closeDialog();
                mErrorLayout.setVisibility(View.VISIBLE);
                ToastUtil.showToast(LogisticsActivity.this, R.string.network_or_server_exception);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.error_retry:
                mErrorLayout.setVisibility(View.GONE);
                getExpressList();
                break;
        }
    }
}
